package cn.jjxx.modules.workflow.controller;

import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.SpringContextHolder;
import cn.jjxx.core.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import cn.jjxx.core.common.controller.BaseBeanController;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresPathPermission;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.jjxx.modules.sys.controller.RoleController;
import cn.jjxx.modules.sys.entity.Menu;
import cn.jjxx.modules.sys.entity.Organization;
import cn.jjxx.modules.sys.service.IOrganizationService;
import cn.jjxx.modules.sys.utils.UserUtils;
import cn.jjxx.modules.workflow.entity.ActMenuOrg;
import cn.jjxx.modules.workflow.service.IActMenuOrgService;

/**   
 * @Title: 模型菜单组织
 * @Description: 模型菜单组织
 * @author zcg
 * @date 2018-03-18 22:01:17
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("${admin.url.prefix}/workflow/actmenuorg")
@RequiresPathPermission("workflow:actmenuorg")
public class ActMenuOrgController extends BaseBeanController<ActMenuOrg> {

    @Autowired
    protected IActMenuOrgService actMenuOrgService;
    @Autowired
	private IOrganizationService organizationService;

    /**
     * @Description: 跳转至模型菜单组织界面，设置流程组织菜单 .<br>
     * @param modelKey 模型的Key.<br>
     * @return String 返回跳转界面.<br> 
     * @author 郑成功 .<br>
     * @date 2018-3-19 上午10:41:36.<br>
     */
    @RequestMapping(value = "modMenuOrg", method = RequestMethod.GET)
    public String update( String modelKey, Model model, HttpServletRequest request,
                              HttpServletResponse response) {
    	List<Menu> menus = SpringContextHolder.getBean(RoleController.class).findUserMenus();
    	List<Menu> list = new ArrayList<Menu>();
    	for(Menu menu:menus){ 
    		if(menu.getIsshow()==1){
    			list.add(menu);
    		}
    	}
    	List<String> orgIds = UserUtils.getOrgChilds(true, true);
    	EntityWrapper<Organization> owrapper = new EntityWrapper<Organization>(Organization.class);
    	owrapper.in("id", orgIds);
    	owrapper.orderBy("code");
    	List<Organization> orgs = organizationService.selectList(owrapper);
    	//获取非当前模型已绑定的菜单组织
    	EntityWrapper<ActMenuOrg> wrapper = new EntityWrapper<ActMenuOrg>(ActMenuOrg.class);
    	wrapper.ne("modelKey", modelKey);
    	List<ActMenuOrg> moList = actMenuOrgService.selectList(wrapper);
    	//修改组织的名称
    	Map<String,List<Map<String,Object>>> menuModels = new LinkedHashMap<String, List<Map<String,Object>>>();
    	for(Organization org:orgs){
    		List<Map<String,Object>> modelMenuList = new ArrayList<Map<String,Object>>();
    		for(ActMenuOrg amo:moList){
    			if(org.getId().equals(amo.getOrgId())){
    				//org.setName(org.getName()+"【"+amo.getModelName()+"-"+amo.getMenuName()+"】");
    				Map<String,Object> map = new LinkedHashMap<String, Object>();
    				map.put("modelName", amo.getModelName());
    				map.put("menuName", amo.getMenuName());
    				modelMenuList.add(map);
    			}
    		}
    		menuModels.put(org.getId(), modelMenuList);
    	}
        model.addAttribute("menus", JSONArray.toJSON(list));
        model.addAttribute("orgs", JSONArray.toJSON(orgs));
        model.addAttribute("selects", JSONArray.toJSON(moList));
        model.addAttribute("data",new ActMenuOrg());
        model.addAttribute("menuModels",JSONArray.toJSON(menuModels));
        return display("modMenuOrg");
    }

    /**
     * @Description: 保存流程菜单组织设置 .<br>
     * @param modelKey 模型的key.<br>
     * @param menuId 菜单Id.<br>
     * @param orgIds 组织Id.<br>
     * @return AjaxJson .<br> 
     * @author 郑成功 .<br>
     * @date 2018-3-19 上午11:29:01.<br>
     */
    @RequestMapping(value = "doSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson doSave(HttpServletRequest request, HttpServletResponse response,
    		String modelKey,String menuId,String[] orgIds ) {
        AjaxJson ajaxJson = new AjaxJson(); 
        ajaxJson.success("保存成功");
        boolean success = actMenuOrgService.saveOrUpdate(modelKey, menuId, orgIds);
        if(!success){
        	ajaxJson.success("保存失败");
        }
        return ajaxJson;
    }
    
    /**
     * @Description: 根据模型的key和菜单Id,获取已绑定的组织 .<br>
     * @param modelKey 模型的key.<br>
     * @param menuId 菜单Id.<br>
     * @return List<ActMenuOrg> 返回已选的组织.<br> 
     * @author 郑成功 .<br>
     * @date 2018-3-19 上午10:36:40.<br>
     */
    @RequestMapping(value = "findSelectedOrgs", method = RequestMethod.POST)
    @ResponseBody
    public List<ActMenuOrg> findSelectedOrgs(HttpServletRequest request, HttpServletResponse response,String modelKey,String menuId) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("保存成功");
        EntityWrapper<ActMenuOrg> wrapper = new EntityWrapper<ActMenuOrg>();
        wrapper.eq("menu_id", menuId);
        //wrapper.eq("model_key", modelKey);
        return actMenuOrgService.selectList(wrapper);
    }
    
    /**
     * @Description: 根据组织Id和菜单Id,获取流程模型的key .<br>
     * @param orgId 组织Id.<br>
     * @param menuId 菜单Id.<br>
     * @return String 返回ModelKey.<br> 
     * @author 郑成功 .<br>
     * @date 2018-3-19 上午11:30:55.<br>
     */
    @RequestMapping(value = "findModelKey", method = RequestMethod.POST)
    @ResponseBody
    public String findModelKey(String orgId,String menuId){
    	EntityWrapper<ActMenuOrg> wrapper = new EntityWrapper<ActMenuOrg>();
        wrapper.eq("menu_id", menuId);
    	if(StringUtils.isNotEmpty(orgId)&&StringUtils.isNotEmpty(menuId)){
    		//没有组织Id,则查询到菜单
	        wrapper.eq("org_id", orgId);
	        ActMenuOrg amo = actMenuOrgService.selectOne(wrapper);
	        if(!ObjectUtils.isNullOrEmpty(amo)){
	        	return amo.getModelKey();
	        }
			return null;
    	}else if(!StringUtils.isNotEmpty(orgId)&&StringUtils.isNotEmpty(menuId)){
    		//如果有组织Id,则查询到组织Id
    		List<ActMenuOrg> list = actMenuOrgService.selectList(wrapper);
	        if(list.size()>0){
	        	return list.get(0).getModelKey();
	        }
    		return null;
    	}
    	return null;
    }

}
